local nongdi = fk.CreateSkill{
  name = "plum__nongdi",

  dynamic_desc = function (self, player, lang)
    local n = player:getMark(self.name)
    return "plum__nongdi_inner:"..n
  end,
}

local DIY = require "packages/diy_utility/diy_utility"
local TuntianUtil = require "packages/tuntian_studio/utility"

Fk:loadTranslationTable{
  [nongdi.name] = "弄笛",
  [":"..nongdi.name] = "出牌阶段内你不能整理手牌；出牌阶段，你可以：①弃置一张牌；②明置左侧第（1）张手牌；"
  .."然后（）中数字改为之点数。若该牌为♣牌，（）内数字由你指定。",

  [":plum__nongdi_inner"] = "你不能整理手牌；出牌阶段你可以：①弃置一张牌；②明置左侧第({1})张手牌；"
  .."然后()中数字改为之点数。若该牌为♣牌，()内数字由你指定。",

  ["#nongdi-discard"] = "弃置一张牌",
  ["#nongdi-reveal"] = "明置左侧第%arg张手牌",
}

--- nongdi combo
---@param player ServerPlayer
---@param i integer
local function updateCombo(player, i)
  local mark = player:getTableMark("nongdi_combo")
  table.insert(mark, i)
  if #mark > 3 then table.remove(mark, 1) end
  player.room:setPlayerMark(player, "nongdi_combo", mark)
end

--- get handcards
---@param player Player
---@return integer[]
local function getTrueSortingHandcards(player)
  return player:getTableMark("nongdi_sorting")
end

--- set handcards
---@param player ServerPlayer
local function setTrueSortingHandcards(player)
  player.room:setPlayerMark(player, "nongdi_sorting", player:getCardIds("h"))
end

--- acquireEffect
nongdi:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, nongdi.name, 1)
  if player.phase == Player.Play then
    player.room:banSortingHandcards(player, "-_nongdi-phase")
    setTrueSortingHandcards(player)
  end
end)

--- loseEffect
nongdi:addLoseEffect(function (self, player, is_death)
  local room = player.room
  room:setPlayerMark(player, "nongdi_combo", 0)
  player.room:unbanSortingHandcards(player, "-nongdi-phase")
end)

--- ban sorting handcards in Play Phase
nongdi:addEffect(fk.EventPhaseStart, {
  can_refresh = function (self, event, target, player, data)
    return player == target and player:hasSkill(nongdi.name) and data.phase == Player.Play
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:banSortingHandcards(player, "-nongdi-phase")
    setTrueSortingHandcards(player)
  end
})

--- update handcards
nongdi:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(nongdi.name)
  end,
  on_refresh = function (self, event, target, player, data)
    setTrueSortingHandcards(player)
  end
})

--- main effect
nongdi:addEffect("active", {
  anim_type = "special",
  interaction = function (self, player)
    local n = player:getMark(nongdi.name)
    local all_choices = {"#nongdi-discard", "#nongdi-reveal:::"..n}
    local choices = table.simpleClone(all_choices)
    if player:getHandcardNum() < n then table.remove(choices, 2) end
    return UI.ComboBox { choices = choices, all_choices = all_choices, }
  end,
  card_num = 1,
  card_filter = function (self, player, to_select, selected)
    if self.interaction.data == "#nongdi-discard" then
      return #selected == 0 and not player:prohibitDiscard(to_select)
    else
      local cards = getTrueSortingHandcards(player)
      local n = player:getMark(nongdi.name)
      return to_select == cards[n] and not TuntianUtil.isShown(to_select)
    end
  end,
  target_num = 0,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    local player = effect.from
    local cardId = effect.cards[1]
    --- updateCombo
    if self.interaction.data == "#nongdi-discard" then
      updateCombo(player, 1)
      room:throwCard(cardId, self.name, player, player)
    else
      updateCombo(player, 2)
      DIY.showCards(player, cardId)
    end
    --- updateNongdiNumber
    if player.dead then return end
    local num
    local card = Fk:getCardById(cardId)
    if card.suit and card.suit == Card.Club then
      local choices = {}
      for i = 1, 99 do
        table.insert(choices, tostring(i))
      end
      num = room:askToChoice(player, {
        choices = choices,
        all_choices = choices,
        cancelable = false,
        prompt = "#nongdi_choose",
        skill_name = nongdi.name,
      })
    elseif card.number and card.number > 0 then
      num = card.number
    end
    --- setPlayerMarks
    if num then
      room:setPlayerMark(player, nongdi.name, tonumber(num))
    end
  end
})

return nongdi